package com.ncut.product.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.ncut.constant.MessageConstant;
import com.ncut.constant.StatusConstant;
import com.ncut.dto.SetmealDTO;
import com.ncut.dto.SetmealPageQueryDTO;
import com.ncut.entity.Setmeal;
import com.ncut.entity.SetmealDish;
import com.ncut.exception.DeletionNotAllowedException;
import com.ncut.result.PageResult;
import com.ncut.vo.DishItemVO;
import com.ncut.vo.SetmealVO;
import com.ncut.product.mapper.SetmealDishMapper;
import com.ncut.product.mapper.SetmealMapper;
import com.ncut.product.service.SetmealService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @FileName SetmealServiceImpl
 * @Description
 * @Author Sword
 * @date 2025-07-09
 **/

@Service
@Slf4j
public class SetmealServiceImpl implements SetmealService {

    @Autowired
    private SetmealMapper setmealMapper;

    @Autowired
    private SetmealDishMapper setmealDishMapper;

    /**
     * 新增套餐
     *
     * @param setmealDTO
     */
    @Override
    @Transactional
    public void saveWithDish(SetmealDTO setmealDTO) {
        Setmeal setmeal = new Setmeal();

        BeanUtils.copyProperties(setmealDTO, setmeal);

        setmealMapper.insert(setmeal);

        List<SetmealDish> setmealDishes = setmealDTO.getSetmealDishes();

        Long id = setmeal.getId();

        if (setmealDishes != null && setmealDishes.size() > 0) {
            setmealDishes.forEach(setmealDish -> {
                setmealDish.setSetmealId(id);
            });

            setmealDishMapper.insertBatch(setmealDishes);
        }


    }

    /**
     * 分页查询
     *
     * @param setmealPageQueryDTO
     * @return
     */
    @Override
    public PageResult pageQuery(SetmealPageQueryDTO setmealPageQueryDTO) {
        PageHelper.startPage(setmealPageQueryDTO.getPage(), setmealPageQueryDTO.getPageSize());

        Page<SetmealVO> page = setmealMapper.pageQuery(setmealPageQueryDTO);

        long total = page.getTotal();
        List<SetmealVO> records = page.getResult();
        return new PageResult(total, records);
    }

    /**
     * 根据id查询套餐和套餐菜品关系
     *
     * @param id
     * @return
     */
    @Override
    public SetmealVO getByIdWithDish(Long id) {
        Setmeal setmeal = setmealMapper.getById(id);

        List<SetmealDish> setmealDishes = setmealDishMapper.getBySetmealId(id);

        SetmealVO setmealVO = new SetmealVO();
        BeanUtils.copyProperties(setmeal, setmealVO);

        setmealVO.setSetmealDishes(setmealDishes);

        return setmealVO;
    }

    /**
     * 修改套餐
     *
     * @param setmealDTO
     */
    @Override
    @Transactional
    public void updateWithDish(SetmealDTO setmealDTO) {
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDTO, setmeal);

        setmealMapper.update(setmeal);

        setmealDishMapper.deleteBySetmealId(setmealDTO.getId());

        List<SetmealDish> setmealDishes = setmealDTO.getSetmealDishes();

        if (setmealDishes != null && setmealDishes.size() > 0) {
            setmealDishes.forEach(setmealDish -> {
                setmealDish.setSetmealId(setmealDTO.getId());
            });

            setmealDishMapper.insertBatch(setmealDishes);
        }

    }

    /**
     * 启售、停售套餐
     *
     * @param status
     * @param id
     */
    @Override
    public void startOrStop(Integer status, Long id) {
        Setmeal setmeal = Setmeal.builder()
                .status(status)
                .id(id)
                .build();
        setmealMapper.update(setmeal);
    }

    /**
     * 批量删除套餐
     *
     * @param ids
     */
    @Override
    @Transactional
    public void deleteBatch(List<Long> ids) {
        //判断当前套餐是否能够被删除---是否存在启售中的套餐
        for (Long id : ids) {
            Setmeal setmeal = setmealMapper.getById(id);

            if (setmeal.getStatus() == StatusConstant.ENABLE) {
                throw new DeletionNotAllowedException(MessageConstant.SETMEAL_ON_SALE);
            }
        }

        setmealMapper.deleteBatch(ids);
        setmealDishMapper.deleteBySetmealIds(ids);

    }

    /**
     * 根据分类id查询套餐和菜品数据
     *
     * @param setmeal
     * @return
     */
    @Override
    public List<SetmealVO> listWithDish(Setmeal setmeal) {
        //查询套餐信息
        List<Setmeal> list = setmealMapper.list(setmeal);

        List<SetmealVO> setmealVOList = new ArrayList<>();
        for (Setmeal s : list) {
            //拷贝套餐信息
            SetmealVO setmealVO = new SetmealVO();
            BeanUtils.copyProperties(s, setmealVO);

            //查询菜品信息
            List<SetmealDish> setmealDishList = setmealDishMapper.getBySetmealId(s.getId());
            setmealVO.setSetmealDishes(setmealDishList);
            setmealVOList.add(setmealVO);
        }

        return setmealVOList;
    }

    /**
     * 根据套餐id查询包含的菜品数据
     *
     * @param id
     * @return
     */
    @Override
    public List<DishItemVO> getDishItemById(Long id) {
        List<DishItemVO> list = setmealMapper.getDishItemById(id);
        return list;
    }

    /**
     * 根据id查询套餐
     *
     * @param id
     * @return
     */
    @Override
    public Setmeal getById(Long id) {
        Setmeal setmeal = setmealMapper.getById(id);
        return setmeal;
    }

    /**
     * 统计套餐启售停售数量
     *
     * @param map
     * @return
     */
    @Override
    public Integer countByMap(Map<String, Object> map) {
        return setmealMapper.countByMap(map);
    }
}
